<template>
    <div class="detail-info">
      <div class="form-item">
        <h4>基本信息</h4>
        <el-form :model="ruleForm" :rules="rules" label-width="120px" ref="ruleForm" class="demo-ruleForm">
          <el-form-item label="分公司" required>
            <el-input v-model="branchName" style="width: 220px" disabled></el-input>
          </el-form-item>
          <el-form-item label="所属部门" required>
            <el-input v-model="orgName" style="width: 220px" disabled></el-input>
          </el-form-item>
          <!--<el-form-item label="所属仓库" required>-->
            <!--<el-select v-model="ruleForm.limit_store_id" filterable>-->
              <!--<el-option-->
                <!--v-for="item in storeList"-->
                <!--:label="item.name"-->
                <!--:value="item.id">-->
              <!--</el-option>-->
            <!--</el-select>-->
          <!--</el-form-item>-->
          <el-form-item label="客资类型" required>
            <el-select v-model="ruleForm.limit_user_type" filterable>
              <el-option
                v-for="item in ruleForm.clientList"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="活动名称" prop="name" required>
            <el-input v-model="ruleForm.name" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="副标题" prop="title">
            <el-input v-model="ruleForm.title" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="活动起止时间" class="is-required">
            <el-date-picker
              style="width: 300px"
              v-model="ruleForm.time"
              type="datetimerange"
              :default-time="['00:00:00', '23:59:59']"
              value-format="yyyy-MM-dd-HH-mm-ss"
              :picker-options="pickerOptions"
              @change="timeChange"
              placeholder="选择时间范围">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="满" prop="name" required>
            <el-input v-model="ruleForm.full" placeholder="请输入金额" style="width: 100px"></el-input>  元
          </el-form-item>
          <el-form-item label="用户可参与次数" prop="num">
            <el-input @blur="" v-model="ruleForm.num" style="width: 150px"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="form-item">
        <h4>加价购商品</h4>
        <el-col :span="2" style="margin-bottom: 10px">
          <el-button type="primary" @click="simpleCheck2">添加商品</el-button>
        </el-col>
        <el-table
          :data="tableDatajia"
          max-height="300"
          border
          style="width: 100%; margin-top: 10px">
          <el-table-column
            prop="productid"
            label="商品ID"
            align="center"
            width="80">
          </el-table-column>
          <el-table-column
            prop="name"
            show-overflow-tooltip
            label="商品名称"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="saleunit"
            label="单位"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="salenorm"
            label="规格"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="saleprice"
            label="系统售价"
            align="center"
            width="80">
          </el-table-column>
          <el-table-column
            prop="cgprice"
            label="最近一次采购价"
            align="center"
            width="120">
          </el-table-column>
          <el-table-column
            label="换购单价"
            align="center"
            width="120">
            <template scope="scope">
              <el-input v-model="scope.row.promotionprice" @blur="val => { return huangouChange(scope.row.promotionprice, scope.row)}"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="数量"
            align="center"
            width="120">
            <template scope="scope">
              <el-input v-model="scope.row.num" @blur="val => { return numChange(scope.row.num, scope.row)}"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="总数量"
            align="center"
            width="120">
            <template scope="scope">
              <el-input v-model="scope.row.totalnum" @blur="val => { return numChange(scope.row.totalnum, scope.row)}"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="促销费用"
            align="center"
            min-width="120">
            <template scope="scope">
              <el-select v-model="scope.row.promotionfeetype" @change="val => { return fyChange2(val, scope.row)}" filterable>
                <el-option
                  v-for="(item, index) in feiyongList"
                  :label="item.promotionName"
                  :value="item.id"
                  :key="index">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="供货商承担金额"
            align="center"
            width="120">
            <template scope="scope">
              <el-input @blur="moneyTest2(scope.row, scope.$index)" v-show="scope.row.isSupplier" v-model="scope.row.providerholdprice"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="230">
            <template scope="scope">
              <el-button @click="omit2(scope.$index, scope.row)" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="form-item">
        <h4>商品信息</h4>
        <el-col :span="2">
          <el-button type="primary" @click="simpleCheck">添加商品</el-button>
        </el-col>
        <el-col :span="6">
          <el-upload
            class="upload-demo"
            :action="fileUrl"
            :on-success="uploadSuccess"
            :on-remove="removeFlie"
            :file-list="fileList"
            accept=".xlsx,.xls">
            <el-button :disabled="primaryDisabled" type="primary">导入表格</el-button>
            <div slot="tip" class="el-upload__tip">只能上传xlsx/xls文件，且只能上传一个文件</div>
          </el-upload>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" size="small" style="">
          <a style="text-decoration:none; color:#fff" href="/static/template/商品活动模板.xlsx">下载模板</a>
        </el-button>
        </el-col>
        <el-table
          :data="tableData"
          max-height="300"
          border
          style="width: 100%; margin-top: 10px">
          <el-table-column
            prop="productid"
            label="商品ID"
            align="center"
            width="80">
          </el-table-column>
          <el-table-column
            prop="name"
            show-overflow-tooltip
            label="商品名称"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="saleunit"
            label="单位"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="salenorm"
            label="规格"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="saleprice"
            label="系统售价"
            align="center"
            width="80">
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="230">
            <template scope="scope">
              <el-button v-show="scope.$index !== 0" @click="top(scope.$index, scope.row)" size="small">置顶</el-button>
              <el-button v-show="scope.$index !== 0" @click="moveT(scope.$index, scope.row)" size="small">上移</el-button>
              <el-button v-show="scope.$index !== (tableData.length - 1)" @click="moveB(scope.$index, scope.row)" size="small">下移</el-button>
              <el-button @click="omit(scope.$index, scope.row)" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="form-item">
        <el-form label-width="180px">
          <h4>备注</h4>
          <el-input  type="textarea"  placeholder="请输入活动说明" v-model="ruleForm.remark" style="width: 500px"></el-input>
        </el-form>
      </div>
      <div style="margin-left: 40%">
        <el-button type="primary" @click="submit($event)">提交</el-button>
        <el-button type="warning" @click="submit($event)">暂存</el-button>
        <el-button type="danger">取 消</el-button>
      </div>
      <!--商品信息-选择单品弹窗-->
      <el-dialog class="dialog" title="选择商品" :visible.sync="dialogSimple">
        <el-form style="margin-top: 20px; overflow: hidden" :inline="true" :model="formSimple" class="demo-form-inline">
          <el-form-item label="商品分类">
            <el-select v-model="formSimple.type1" style="width: 150px" filterable clearable @change="checkType2(formSimple.type1)">
              <el-option
                v-for="item in productList1"
                :label="item.cateName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="formSimple.type2" style="width: 150px" filterable clearable @change="checkType3(formSimple.type2)">
              <el-option
                v-for="item in productList2"
                :label="item.cateName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="formSimple.type3" style="width: 150px" filterable clearable>
              <el-option
                v-for="item in productList3"
                :label="item.cateName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="品牌">
            <el-select v-model="formSimple.brand" style="width: 150px" filterable clearable>
              <el-option
                v-for="item in brandList"
                :label="item.brandName"
                :value="item.brandId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品ID">
            <el-input style="width: 100px" v-model="formSimple.ID"></el-input>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input style="width: 120px" v-model="formSimple.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="simpleList">搜索</el-button>
          </el-form-item>
        </el-form>
        <div>
          <el-button @click="lotAdd">批量添加</el-button>
        </div>
        <el-table
          :data="tableData2"
          v-loading="isLoading"
          height="200"
          ref="multipleTable"
          border
          style="width: 100%"
          @select-all="handleSelectionChange"
          @selection-change="handleChange">
          <el-table-column
            type="selection"
            :selectable="isCheck"
            width="55">
          </el-table-column>
          <el-table-column
            prop="hmpId"
            label="商品ID"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="hmpName"
            label="商品名称"
            align="center"
            width="200">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="saleunit"
            label="单位"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="salenorm"
            label="规格"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="price"
            label="系统售价"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="proBuyPrice"
            label="最近一次采购价"
            align="center"
            width="150">
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="100">
            <template scope="scope">
              <el-button v-show="scope.row.name === 'add' && scope.row.isActivity === ''" @click="add(scope.$index, scope.row, $event)" size="small">添加</el-button>
              <el-button v-show="scope.row.name === 'cancel' && scope.row.isActivity === ''" @click="add(scope.$index, scope.row, $event)" size="small">撤销</el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="isActivity"
            label="当前活动"
            align="center"
            width="100">
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handleCurrentChange"
          :page-size="Number(pageSize)"
          layout="total, prev, pager, next"
          :total="Number(total)"
          style="padding-top:10px;text-align: right;">
        </el-pagination>
      </el-dialog>
      <!--加价购商品-选择单品弹窗-->
      <el-dialog class="dialog" title="选择商品" :visible.sync="dialogSimple2">
        <el-form style="margin-top: 20px; overflow: hidden" :inline="true" :model="formSimple" class="demo-form-inline">
          <el-form-item label="商品分类">
            <el-select v-model="formSimple.type1" style="width: 150px" filterable clearable @change="checkType2(formSimple.type1)">
              <el-option
                v-for="item in productList1"
                :label="item.cateName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="formSimple.type2" style="width: 150px" filterable clearable @change="checkType3(formSimple.type2)">
              <el-option
                v-for="item in productList2"
                :label="item.cateName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="formSimple.type3" style="width: 150px" filterable clearable>
              <el-option
                v-for="item in productList3"
                :label="item.cateName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="品牌">
            <el-select v-model="formSimple.brand" style="width: 150px" filterable clearable>
              <el-option
                v-for="item in brandList"
                :label="item.brandName"
                :value="item.brandId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品ID">
            <el-input style="width: 100px" v-model="formSimple.ID"></el-input>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input style="width: 120px" v-model="formSimple.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="simpleList2">搜索</el-button>
          </el-form-item>
        </el-form>
        <div>
          <el-button @click="lotAdd2">批量添加</el-button>
        </div>
        <el-table
          :data="tableData3"
          v-loading="isLoading2"
          height="200"
          ref="multipleTable2"
          border
          style="width: 100%"
          @select-all="handleSelectionChange2"
          @selection-change="handleChange2">
          <el-table-column
            type="selection"
            :selectable="isCheck"
            width="55">
          </el-table-column>
          <el-table-column
            prop="hmpId"
            label="商品ID"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="hmpName"
            label="商品名称"
            align="center"
            width="200">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="saleunit"
            label="单位"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="salenorm"
            label="规格"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="price"
            label="系统售价"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="proBuyPrice"
            label="最近一次采购价"
            align="center"
            width="150">
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="100">
            <template scope="scope">
              <el-button v-show="scope.row.name === 'add' && scope.row.isActivity === ''" @click="add2(scope.$index, scope.row, $event)" size="small">添加</el-button>
              <el-button v-show="scope.row.name === 'cancel' && scope.row.isActivity === ''" @click="add2(scope.$index, scope.row, $event)" size="small">撤销</el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="isActivity"
            label="当前活动"
            align="center"
            width="100">
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handleCurrentChange2"
          :page-size="Number(pageSize2)"
          layout="total, prev, pager, next"
          :total="Number(total2)"
          style="padding-top:10px;text-align: right;">
        </el-pagination>
      </el-dialog>
      </div>
    </div>
</template>
<script>
  import { mapState } from 'vuex'
  import { apiHost, apiURL } from '../../../../../config/settings.js'
  import mixins from '../../../../../components/mixins.vue'
  import { cms, activity } from '../../../../../config/proxy.js'
  export default {
    mixins: [mixins],
    data () {
      var checkName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('活动名称不能为空'))
        } else {
          callback()
        }
      }
      var checkTitle = (rule, value, callback) => {
        if (value.length > 20) {
          return callback(new Error('不能超过20个字'))
        } else {
          callback()
        }
      }
      var checkNum = (rule, value, callback) => {
        if (value && value <= 0) {
          callback(new Error('参与频次必须大于0'))
        } else {
          callback()
        }
      }
      return {
        pickerOptions: {
          disabledDate (time) {
            return time.getTime() < new Date().getTime()
          }
        },
        rules: {
          title: [
            { validator: checkTitle, trigger: 'blur' }
          ],
          name: [
            { validator: checkName, trigger: 'blur' }
          ],
          num: [
            { validator: checkNum, trigger: 'blur' }
          ]
        },
//      基本信息
        data: '',
        ruleForm: {
          clientList: [{
            id: 1,
            name: '普通超市'
          }],
          limit_store_id: '',
          limit_user_type: 1,
          name: '',
          title: '',
          time: '',
          start_time: '',
          end_time: '',
          full: '',
          num: '',
          remark: ''
        },
//      商品信息---选择单品
        dialogSimple: false,
        brandList: [],
        productList1: [],
        productList2: [],
        productList3: [],
        formSimple: {
          type1: '',
          type2: '',
          type3: '',
          brand: '',
          ID: '',
          name: ''
        },
        isLoading: false,
        tableData2: [],
        total: '',
        page: 1,
        pageSize: 10,
        all: [],
//      加价购商品---选择单品
        dialogSimple2: false,
        isLoading2: false,
        tableData3: [],
        total2: '',
        page2: 1,
        pageSize2: 10,
        all2: [],
//      导入表格
        fileList: [],
        fileUrl: '',
        primaryDisabled: false,
//      商品信息
        tableData: [],
//      加价购商品
        tableDatajia: [],
//      费用列表
        feiyongList: [],
//      详情信息
        detailData: {}
      }
    },
    methods: {
//      moneyTest (row, index) {
//        const val = row.providerholdprice
//        var str = val.indexOf('.')
//        var numlength = 0
//        if (str !== -1) {
//          const index = val.substring(str + 1, val.length)
//          numlength = index.length
//        }
//        if (isNaN(val) || (val < 0) || (numlength > 2)) {
//          this.$message({
//            showClose: true,
//            message: '供货商承担金额大于0, 最多为两位小数',
//            type: 'error'
//          })
//          row.providerholdprice = ''
//        }
//      }, // 供货商承担金额验证----大于等于0, 两位小数
//      fyChange (val, row) {
//        this.feiyongList.forEach((item, index) => {
//          if (item.id === val) {
//            if (item.promotionType === 1) {
//              row.isSupplier = false
//            } else {
//              row.isSupplier = true
//            }
//          }
//        })
//      },  // 促销费用变化
      timeChange (val) {
        this.ruleForm.start_time = val.slice(0, 19)
        this.ruleForm.end_time = val.slice(22)
        activity.cxfy.allList({branchId: this.branchId, startTime: this.ruleForm.start_time, endTime: this.ruleForm.end_time}, (res) => {
          if (res.message === 'ok') {
            this.feiyongList = res.content
          }
        }) // 获取促销费用
      }, // 时间变化
      simpleCheck () {
        if (this.ruleForm.time.length === 0) {
          this.$message({
            showClose: true,
            message: '请先选择时间',
            type: 'error'
          })
          return false
        }
        this.formSimple.ID = ''
        this.formSimple.name = ''
        this.formSimple.type1 = ''
        this.formSimple.type2 = ''
        this.formSimple.type3 = ''
        this.formSimple.brand = ''
        this.tableData2 = []
        this.total = ''
        this.dialogSimple = true
      },   // 添加商品弹窗
      checkType2 (val) {
        this.productList2 = []
        this.formSimple.type2 = ''
        this.formSimple.brand = ''
        cms.pType({pid: val}, (res) => {
          if (res.message === 'ok') {
            this.productList2 = res.content
          }
        })
      }, // 二级分类
      checkType3 (val) {
        this.productList3 = []
        this.formSimple.type3 = ''
        cms.pType({pid: val}, (res) => {
          if (res.message === 'ok') {
            this.productList3 = res.content
          }
        })
        cms.brand({cateId: val}, (res) => {
          if (res.message === 'ok') {
            this.brandList = res.content
          }
        })
      }, // 三级分类
      handleChange (all) {
        this.all = all
      }, // 多选
      handleSelectionChange (all) {
        this.all = all
      }, // 多选
      isCheck (row, index) {
        return !row.isActivity && row.name === 'add'
      }, // 选择框置灰
      omit (index, row) {
        this.tableData.splice(index, 1)
      },  // 删除
      top (index, row) {
        this.tableData.splice(0, 0, row)
        this.tableData.splice(index + 1, 1)
      },   // 置顶
      moveT (index, row) {
        this.tableData.splice(index - 1, 0, row)
        this.tableData.splice(index + 1, 1)
      }, // 上移
      moveB (index, row) {
        this.tableData.splice(index + 2, 0, row)
        this.tableData.splice(index, 1)
      }, // 下移
      add (index, row, event) {
        if (event.target.innerText === '添加') {
          row.name = 'cancel'
          this.$refs.multipleTable.toggleRowSelection(row, true)
          const arr = {
            productid: row.hmpId,   // 商品ID
            name: row.hmpName,      // 商品名称
            saleunit: row.saleunit, // 销售单位
            salenorm: row.salenorm, // 销售规格
            saleprice: row.price,   // 系统售价
            saleunit_convert: row.saleUnitConvert, // 销售单位和最小单位转换值
            cgunit_convert: row.CGUnitConvert,     // 采购单位和最小单位转换值
            cgprice: row.proBuyPrice               // 最近一次采购价
          }
          this.tableData.unshift(arr)
        } else {
          row.name = 'add'
          this.$refs.multipleTable.toggleRowSelection(row, false)
          const index = this.tableData.indexOf(row)
          this.tableData.splice(index, 1)
        }
      },  // 单条添加
      lotAdd () {
//      状态变为撤销
        this.tableData2.forEach((item1, index1) => {
          this.all.forEach(function (item, index) {
            if (item1.hmpId === item.hmpId) {
              item1.name = 'cancel'
            }
          })
        })
//      添加进去
        this.all.forEach((item, index) => {
          if (this.tableData.indexOf(item) === -1) {
            this.tableData.unshift(item)
          }
        })
      },                // 批量添加
      simpleList () {
        this.isLoading = true
        const queryData = {
          branchId: this.branchId,
          userType: this.ruleForm.limit_user_type,
          promotionType: 4, // 活动类型
          flag: 1,
          isMaster: 1,
          fourCate: this.formSimple.brand,
          page: this.page,
          pageSize: this.pageSize,
          hmpId: this.formSimple.ID,
          hmpName: this.formSimple.name,
          firstCate: this.formSimple.type1,
          secondCate: this.formSimple.type2,
          thirdCate: this.formSimple.type3,
          startTime: this.ruleForm.start_time,
          endTime: this.ruleForm.end_time
        }
        activity.yhqhd.jcq.ppq_productList(queryData, (res) => {
          if (res.message === 'ok' && res.content.content !== null) {
            res.content.content.forEach((item, index) => {
              item.isActivity = item.isActivity.join(',')
            })
            console.log(res, '商品分页数据')
            this.isLoading = false
            if (this.tableData.length !== 0) {
              for (var i = 0; i < res.content.content.length; i++) {
                let a = true
                for (var j = 0; j < this.tableData.length; j++) {
                  if (res.content.content[i].hmpId === this.tableData[j].productid) {
                    res.content.content[i].name = 'cancel'
                    a = false
                  } else {
                    res.content.content[i].name = 'add'
                  }
                  if (a === false) {
                    j = this.tableData.length
                  }
                }
              }
            } else {
              res.content.content.forEach(function (item, index) {
                item.name = 'add'
              })
            }
            this.tableData2 = res.content.content
            this.total = res.content.count
          } else if (res.message === 'ok' && res.content.content === null) {
            this.tableData2 = []
            this.total = ''
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
            this.total = ''
          }
        })
      },            // 商品分页查询
      handleCurrentChange (val) {
        this.page = val
        this.simpleList()
      }, // 分页
      // ------------------------加价购-------------------
      fyChange2 (val, row) {
        this.feiyongList.forEach((item, index) => {
          if (item.id === val) {
            if (item.promotionType === 1) {  // 惠民全担
              row.providerholdprice = 0
              row.isSupplier = false
            } else if (item.promotionType === 2) { // 惠民及供货商
              row.feiyongtype = 2
              row.isSupplier = true
            } else { // 供货商全担
              row.feiyongtype = 3
              row.isSupplier = true
            }
          }
        })
      },     // 促销费用变化
      huangouChange (val, row) {
        var str = val.indexOf('.')
        var numlength = 0
        if (str !== -1) {
          const index = val.substring(str + 1, val.length)
          numlength = index.length
        }
        if (isNaN(val) || (val < 0) || (numlength > 2) || val > row.saleprice) {
          this.$message({
            showClose: true,
            message: '换购单价不能小于0, 最多为两位小数, 且不能大于销售价格',
            type: 'error'
          })
          row.promotionprice = ''
        }
      }, // 验证：换购单价
      numChange (val, row) {
        var str = val.indexOf('.')
        if (isNaN(val) || (val <= 0) || (str !== -1)) {
          this.$message({
            showClose: true,
            message: '必须大于0, 不能含小数',
            type: 'error'
          })
          row.num === val ? row.num = '' : row.totalnum = ''
        }
      },     // 验证：数量 / 总数
      moneyTest2 (row, index) {
        const val = row.providerholdprice
        var str = val.indexOf('.')
        var numlength = 0
        if (str !== -1) {
          const index = val.substring(str + 1, val.length)
          numlength = index.length
        }
        if (row.feiyongtype === 2) {
          if (isNaN(val) || (val < 0) || (numlength > 2)) {
            this.$message({
              showClose: true,
              message: '供货商承担金额必须大于等于0, 最多为两位小数',
              type: 'error'
            })
            row.providerholdprice = ''
          }
        } else {
          const maosun = row.saleprice - row.promotionprice
          if (val < maosun) {
            this.$message({
              showClose: true,
              message: '供货商承担金额必须大于等于毛利损失',
              type: 'error'
            })
            row.providerholdprice = ''
          }
        }
      },  // 验证：供货商承担金额
      add2 (index, row, event) {
        if (event.target.innerText === '添加') {
          row.name = 'cancel'
          this.$refs.multipleTable2.toggleRowSelection(row, true)
          const arr = {
            productid: row.hmpId,   // 商品ID
            name: row.hmpName,      // 商品名称
            saleunit: row.saleunit, // 销售单位
            salenorm: row.salenorm, // 销售规格
            saleprice: row.price,   // 系统售价
            cgprice: row.proBuyPrice, // 最近一次采购价
            promotionprice: '',     // 换购单价
            num: '',     // 数量
            totalnum: '',     // 总数量
            promotionfeetype: '',   // 费用Id
            providerholdprice: '',  // 供货商承担金额
            isSupplier: true,
            feiyongtype: '',        // 费用类型
            saleunit_convert: row.saleUnitConvert, // 销售单位和最小单位转换值
            cgunit_convert: row.CGUnitConvert     // 采购单位和最小单位转换值
          }
          this.tableDatajia.unshift(arr)
        } else {
          row.name = 'add'
          this.$refs.multipleTable2.toggleRowSelection(row, false)
          const index = this.tableDatajia.indexOf(row)
          this.tableDatajia.splice(index, 1)
        }
      },  // 单条添加
      lotAdd2 () {
//      状态变为撤销
        this.tableData3.forEach((item1, index1) => {
          this.all2.forEach(function (item, index) {
            if (item1.hmpId === item.hmpId) {
              item1.name = 'cancel'
            }
          })
        })
//      添加进去
        this.all2.forEach((item, index) => {
          if (this.tableDatajia.indexOf(item) === -1) {
            this.tableDatajia.unshift(item)
          }
        })
      },                // 批量添加
      simpleList2 () {
        this.isLoading2 = true
        const queryData = {
          branchId: this.branchId,
          userType: this.ruleForm.limit_user_type,
          promotionType: 6, // 活动类型
          flag: 1,
          isMaster: 1,
          fourCate: this.formSimple.brand,
          page: this.page2,
          pageSize: this.pageSize2,
          hmpId: this.formSimple.ID,
          hmpName: this.formSimple.name,
          firstCate: this.formSimple.type1,
          secondCate: this.formSimple.type2,
          thirdCate: this.formSimple.type3,
          startTime: this.ruleForm.start_time,
          endTime: this.ruleForm.end_time
        }
        activity.yhqhd.jcq.ppq_productList(queryData, (res) => {
          if (res.message === 'ok' && res.content.content !== null) {
            res.content.content.forEach((item, index) => {
              item.isActivity = item.isActivity.join(',')
            })
            console.log(res, '商品分页数据')
            this.isLoading2 = false
            if (this.tableDatajia.length !== 0) {
              for (var i = 0; i < res.content.content.length; i++) {
                let a = true
                for (var j = 0; j < this.tableData3.length; j++) {
                  if (res.content.content[i].hmpId === this.tableData3[j].productid) {
                    res.content.content[i].name = 'cancel'
                    a = false
                  } else {
                    res.content.content[i].name = 'add'
                  }
                  if (a === false) {
                    j = this.tableData3.length
                  }
                }
              }
            } else {
              res.content.content.forEach(function (item, index) {
                item.name = 'add'
              })
            }
            this.tableData3 = res.content.content
            this.total2 = res.content.count
          } else if (res.message === 'ok' && res.content.content === null) {
            this.tableData3 = []
            this.total2 = ''
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
            this.total2 = ''
          }
        })
      },            // 商品分页查询
      handleCurrentChange2 (val) {
        this.page2 = val
        this.simpleList2()
      }, // 分页
      handleChange2 (all) {
        this.all2 = all
      },          // 多选
      handleSelectionChange2 (all) {
        this.all2 = all
      }, // 多选
      simpleCheck2 () {
        if (this.ruleForm.time === '') {
          this.$message({
            showClose: true,
            message: '请先选择时间',
            type: 'error'
          })
          return false
        }
        this.formSimple.ID = ''
        this.formSimple.name = ''
        this.formSimple.type1 = ''
        this.formSimple.type2 = ''
        this.formSimple.type3 = ''
        this.formSimple.brand = ''
        this.tableData3 = []
        this.total2 = ''
        this.dialogSimple2 = true
      },   // 添加商品弹窗
      omit2 (index, row) {
        this.tableDatajia.splice(index, 1)
      },  // 删除
      // ------------------------------------------------------------------------
      uploadSuccess (res, file, fileList) {
        console.log(res)
        if (res.message === 'ok') {
          this.tableData = res.content
          this.primaryDisabled = true
        } else {
          this.primaryDisabled = false
          this.fileList = []
          this.$message({
            showClose: true,
            message: '上传失败',
            type: 'error'
          })
        }
      }, // 导入表格
      removeFlie (file, fileList) {
        if (fileList.length === 0) {
          this.primaryDisabled = false
          this.tableData = []
        }
      },       // 删除表格
      // ---------------------------------------------------------------------
      editIn (id) {
        activity.sphd.detail({id: id}, (res) => {
          console.log(res, '加价购详情')
          if (res.message === 'ok') {
            this.detailData = res.content
            // 数据赋值
            this.ruleForm.name = res.content.promotion_name
            this.ruleForm.title = res.content.sub_promotion_name
            this.ruleForm.full = res.content.limit_full_money
            this.ruleForm.num = res.content.limit_attend_count
            this.ruleForm.time = [res.content.start_time, res.content.end_time]
            this.ruleForm.remark = res.content.remark
            this.tableData = res.content.main_product
            this.tableDatajia = res.content.vice_product
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
        })
      }, // 编辑回写
      submit (event) {
//        if (this.tableData.length === 0) {
//          this.$message({
//            showClose: true,
//            message: '商品信息至少为一条',
//            type: 'error'
//          })
//          return false
//        }
        const Data = {
          end_time: this.ruleForm.end_time,
          start_time: this.ruleForm.start_time,
          gwId: this.roleId,
          iscaogao: event.target.innerText === '提交' ? 2 : 1,
          limit_attend_count: this.ruleForm.num,
          limit_branch_id: this.branchId,
          limit_department: this.orgId,
          limit_full_money: this.ruleForm.full,
          limit_store_id: this.ruleForm.limit_store_id,
          limit_type: this.ruleForm.limit_type,
          limit_user_type: this.ruleForm.limit_user_type,
          newBranchId: this.bid,
          product: this.tableData,
          productIncrease: this.tableDatajia,
          promotion_name: this.ruleForm.name,
          remark: this.ruleForm.remark,
          sub_title: this.ruleForm.title,
          uorId: this.uorId,
          userId: this.userId,
          userName: this.userName
        }
        console.log(Data, '提交数据')
        if (!this.data || this.$store.state.tab.menuId === 'jiajiagoufz') {
          activity.sphd.myj.jiajiagou.create(Data, (res) => {
            if (res.code === 0) {
              if (this.$store.state.tab.menuId === 'jiajiagoufz') {
                this.$store.dispatch('delTab', {id: 'jiajiagoufz'})
              } else {
                this.$store.dispatch('delTab', {id: 'jiajiagouAdd'})
              }
              this.$store.dispatch('jiajiagouState', this.$store.state.updateState.jiajiagouState + 1)
              this.$message({
                showClose: true,
                message: res.message,
                type: 'success'
              })
            } else {
              this.$message({
                showClose: true,
                message: res.message,
                type: 'error'
              })
            }
          })
        } else {
          Data.id = this.data.id
          Data.promotion_id = this.data.promotion_id
          activity.sphd.myj.jiajiagou.edit(Data, (res) => {
            if (res.code === 0) {
              this.$store.dispatch('delTab', {id: 'jiajiagoubj'})
              this.$store.dispatch('jiajiagouState', this.$store.state.updateState.jiajiagouState + 1)
              this.$message({
                showClose: true,
                message: res.message,
                type: 'success'
              })
            } else {
              this.$message({
                showClose: true,
                message: res.message,
                type: 'error'
              })
            }
          })
        }
      } // 提交 / 暂存
    },
    computed: mapState({
      userName: function (state) {
        return state.user.info.user_name
      },
      userId: function (state) {
        return state.user.info.user_id
      },
      orgName: function (state) {
        return state.user.info.parOrgName
      },
      orgId: function (state) {
        return state.user.info.or_org_id
      },
      roleId: function (state) {
        return Number(state.user.info.roleid)
      },
      uorId: function (state) {
        return Number(state.user.info.uor_id)
      },
      branchName: function (state) {
        return state.user.info.branchId === 1 ? '总公司' : state.user.info.branchName
      },
      branchId: function (state) {
        return state.user.info.branchId
      },
      bid: function (state) {
        return state.user.info.branchOrgId
      }
    }),
    created () {
      this.fileUrl = 'http://' + apiHost.newCms + ':20000' + apiURL.manjian_upload // 导入表格
      cms.pType({pid: 0}, (res) => {
        if (res.message === 'ok') {
          this.productList1 = res.content
        }
      }) // 商品一级分类
      this.data = this.$store.state.tab.uid // row
      if (this.data) {
        this.editIn(this.data.id)
      } // 数据回显
    }
  }
</script>
<style>
  .dialog .el-form--inline div {
    display: inline-block;
    float: left;
  }
  .stair {
    margin-left: 66px;
    font-size: 14px;
    color: #48576a;
  }
</style>
